package view;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import model.IExchangerModel;
import view.table.ExchangerTableCellEditor;
import view.table.ExchangerTableModel;
import view.table.ExchangerTableRenderer;
import controller.IExchangerController;

public class CurrenciesTableDialog extends JDialog implements ActionListener {

	/**
         * 
         */
	private static final long serialVersionUID = 1L;
	private IExchangerModel model;
	private ExchangerView view;
	private ResourceBundleWrapper resourceBundleWrapper;
	private JTable table;
	private IExchangerController control;
	private JPanel buttonPan;
	private JButton selectAllButton, unSelectAllButton, deleteButton,
			addButton, closeButton;
	private AddCurrencyDialog addCurrenciesDialog;

	public CurrenciesTableDialog(ExchangerView view, IExchangerController control,
			IExchangerModel model, ResourceBundleWrapper resourceBundleWrapper) {
		super();
		this.view = view;
		this.model = model;
		this.control = control;
		this.resourceBundleWrapper = resourceBundleWrapper;
		this.setTitle(this.resourceBundleWrapper.getString("modify"));
		this.setSize(640, 200);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(view);
		this.setLocation((int)(this.getLocation().getX()-((int)this.getWidth())), (int)this.getLocation().getY());
		this.buildContentPane();
		this.setVisible(true);
	}

	private void buildContentPane() {
		// Panel
		JPanel pan = new JPanel(new BorderLayout());
		buildButtons();
		// Table
		Object[][] data = model.getAllCurrenciesInfos();
		String title[] = { this.resourceBundleWrapper.getString("logo"),
				this.resourceBundleWrapper.getString("currencyName"),
				this.resourceBundleWrapper.getString("currencyCode"),
				this.resourceBundleWrapper.getString("rate") };
		this.table = new JTable(new ExchangerTableModel(data, title,
				this.resourceBundleWrapper.getString("select")));
		this.table.getTableHeader().setReorderingAllowed(false);
		this.table.setDefaultRenderer(String.class, new ExchangerTableRenderer(
				this.resourceBundleWrapper));
		this.table.setDefaultEditor(String.class, new ExchangerTableCellEditor(
				this.control, new JTextField()));
		pan.add(new JScrollPane(table), BorderLayout.CENTER);
		pan.add(this.buttonPan, BorderLayout.SOUTH);
		this.getContentPane().add(pan);
	}

	private void buildButtons() {
		this.buttonPan = new JPanel();
		selectAllButton = new JButton(
				this.resourceBundleWrapper.getString("selectAll"));
		selectAllButton.setToolTipText(this.resourceBundleWrapper
				.getString("selectAllButton"));
		selectAllButton.addActionListener(this);
		this.buttonPan.add(selectAllButton);

		unSelectAllButton = new JButton(
				this.resourceBundleWrapper.getString("unSelectAll"));
		unSelectAllButton.setToolTipText(this.resourceBundleWrapper
				.getString("unselectAllButton"));
		unSelectAllButton.addActionListener(this);
		this.buttonPan.add(unSelectAllButton);

		deleteButton = new JButton(
				this.resourceBundleWrapper.getString("delete"));
		deleteButton.setToolTipText(this.resourceBundleWrapper
				.getString("deleteButton"));
		deleteButton.addActionListener(this);
		this.buttonPan.add(deleteButton);

		addButton = new JButton(this.resourceBundleWrapper.getString("add"));
		addButton.setToolTipText(this.resourceBundleWrapper
				.getString("addButton"));
		addButton.addActionListener(this);
		this.buttonPan.add(addButton);

		closeButton = new JButton(this.resourceBundleWrapper.getString("close"));
		closeButton.setToolTipText(this.resourceBundleWrapper
				.getString("closeButton"));
		closeButton.addActionListener(this);
		this.buttonPan.add(closeButton);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.closeButton) {
			this.dispose();
		} else if (e.getSource() == this.selectAllButton) {
			for (int i = 0; i < this.table.getRowCount(); i++)
				this.table.getModel().setValueAt(true, i, 0);

		} else if (e.getSource() == this.unSelectAllButton) {
			for (int i = 0; i < this.table.getRowCount(); i++)
				this.table.getModel().setValueAt(false, i, 0);

		} else if (e.getSource() == this.addButton) {
			addCurrenciesDialog = new AddCurrencyDialog(view, control,
					resourceBundleWrapper, this);
		} else if (e.getSource() == this.deleteButton) {
			int count = 0;
			for (int i = 0; i < this.table.getRowCount(); i++) {
				if ((Boolean) this.table.getModel().getValueAt(i, 0)) {
					String currencyName = (String) this.table.getModel()
							.getValueAt(i, 1);
					this.control.removeCurrency(currencyName);
					count++;
				}
			}
			if (count > 0) {
				this.refresh();
			}
		}
	}

	public void refresh() {
		((ExchangerTableModel) this.table.getModel()).refresh(
				this.model.getAllCurrenciesInfos(),
				this.resourceBundleWrapper.getString("select"));
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (this.resourceBundleWrapper != null) {
			this.setTitle(this.resourceBundleWrapper.getString("modify"));
			this.selectAllButton.setText(this.resourceBundleWrapper
					.getString("selectAll"));
			this.selectAllButton.setToolTipText(this.resourceBundleWrapper
					.getString("selectAllButton"));
			this.unSelectAllButton.setText(this.resourceBundleWrapper
					.getString("unSelectAll"));
			this.unSelectAllButton.setToolTipText(this.resourceBundleWrapper
					.getString("unselectAllButton"));
			this.deleteButton.setText(this.resourceBundleWrapper
					.getString("delete"));
			this.deleteButton.setToolTipText(this.resourceBundleWrapper
					.getString("deleteButton"));
			this.addButton.setText(this.resourceBundleWrapper.getString("add"));
			this.addButton.setToolTipText(this.resourceBundleWrapper
					.getString("addButton"));
			this.closeButton.setText(this.resourceBundleWrapper
					.getString("close"));
			closeButton.setToolTipText(this.resourceBundleWrapper
					.getString("closeButton"));
			Object[][] data = model.getAllCurrenciesInfos();
			String title[] = { this.resourceBundleWrapper.getString("logo"),
					this.resourceBundleWrapper.getString("currencyName"),
					this.resourceBundleWrapper.getString("currencyCode"),
					this.resourceBundleWrapper.getString("rate") };
			((ExchangerTableModel) this.table.getModel()).update(data, title,
					this.resourceBundleWrapper.getString("select"));
		}
		if (this.addCurrenciesDialog != null) {
			this.addCurrenciesDialog.repaint();
		}
	}

}
